<?php

namespace App\Http\Requests\PartTime;

use App\Http\Requests\Request;

/**
 * 兼职创建编辑校验
 * Class PartTimeCreateRequest
 * @package App\Http\Requests\PartTime
 */
class PartTimeCreateRequest extends Request
{

    public function authorize()
    {
        return true;
    }

    public function response(array $errors)
    {
        $tmp = '';
        foreach ($errors as $k => $v) {
            if ($v[0] == $tmp) {
                unset($errors[$k]);
            } else {
                $tmp = $v[0];
            }
        }
        return parent::response($errors);
    }

    public function rules()
    {
        $input = $this->all();
        if ($this->input('long_term', 0) == 0) {
            $long_term_check = 'required';
            $time_end_check = '';
        } else {
            $long_term_check = '';
            if (strtotime($this->input('time_end')) <= strtotime($this->input('time_start'))) {
                $time_end_check = 'after:time_start';
            } else {
                $time_end_check = '';
            }
        }

        return [
            'title' => 'required',
            'type_id' => 'bail|required|numeric',
            'time_start' => 'bail|' . $long_term_check,
            'time_end' => 'bail|' . $long_term_check . $time_end_check,
            'need_num' => 'bail|required|numeric',
            'sex' => 'bail|required|numeric',
            'education' => 'bail|required|numeric',
            'salary_price' => 'bail|required|numeric',
            'salary_unit' => 'bail|required|numeric',
            'salary_method' => 'bail|required|numeric',
            'city_id' => 'required|numeric',
            'address' => 'required',
            'content' => 'required',
            'contacts' => 'required',
            'phone' => 'check_phone:'.$input['area_code'].','.$input['ext'],
            'num' => 'free_time_check'
        ];
    }

    public function messages()
    {
        return [
            'title.required' => '兼职名称名称必须填写',
            'type_id.required' => '请选择兼职类型',
            'type_id.numeric' => '请选择兼职类型',
            'time_start.required' => '请选择工作周期开始日期',
            'time_end.required' => '请选择工作周期结束日期',
            'time_end.after' => '结束日期要大于等于开始日期',
            'need_num.required' => '请填写招聘人数',
            'need_num.numeric' => '请填写招聘人数',
            'sex.required' => '请选择性别要求',
            'sex.numeric' => '请选择性别要求',
            'education.required' => '请选择学历要求',
            'education.numeric' => '请选择学历要求',
            'salary_price.required' => '请填写工资待遇金额',
            'salary_price.numeric' => '请填写工资待遇金额',
            'salary_unit.required' => '请选择金额单位',
            'salary_unit.numeric' => '请选择金额单位',
            'salary_method.required' => '请选择结算方式',
            'salary_method.numeric' => '请选择结算方式',
            'city_id.required' => '请选择工作地点',
            'city_id.numeric' => '请选择工作地点',
            'address.required' => '请填写工作地址',
            'content.required' => '请填写兼职详情',
            'contacts.required' => '请填写联系人',
            'phone.check_phone'=>'请输入正确的区号和电话号码',
            'num.free_time_check' => '请选择工作时间'
        ];
    }
}